﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.netdisk
{
    public partial class _new :  ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    
        protected void Page_Load(object sender, EventArgs e)
        {

            NetdiskDao dao;
            if (!this.Page.IsPostBack)
            {
                return;
            }
            this.pnlMain.Visible = false;
            string path = this.txtDiskPath.Value;
            if (path != string.Empty)
            {
                if (path.EndsWith(@"\"))
                {
                    path = path.Substring(0, path.Length - 1);
                }
                try
                {
                    if (!Directory.Exists(path))                   
                    {
                        this.litMessage.Text = base.ShowError("目录路径不正确！");
                        return;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.litMessage.Text = base.ShowError("目录路径不正确！");
                    return;
                }
              
            }
       
            dao = new NetdiskDao();
            NetdiskData olll = new NetdiskData();
            olll.Name = this.txtName.Value;
            olll.DiskPath = path;
            olll.Type = Convert.ToInt32(this.ddlType.Value);
            if (this.PostDeptId.Value != string.Empty)
            {
                olll.PostDept = this.PostDeptId.Value;
            }
            if (this.AccessUserId.Value != string.Empty)
            {
                olll.AccessUser = this.AccessUserId.Value;
            }
            string str2 = string.Empty;
            foreach (ListItem item in this.ckblDefaultPriv.Items)
            {
                if (item.Selected)
                {
                    str2 = str2 + item.Value + ",";
                }
            }
            if (str2 != string.Empty)
            {
                olll.DefaultPriv = str2;
            }
            if (this.NewUserId.Value != string.Empty)
            {
                olll.NewUser = this.NewUserId.Value;
            }
            if (this.ReadUserId.Value != string.Empty)
            {
                olll.ReadUser = this.ReadUserId.Value;
            }
            string str3 = string.Empty;
            foreach (ListItem item2 in this.ckblReadPriv.Items)
            {
                if (item2.Selected)
                {
                    str3 = str3 + item2.Value + ",";
                }
            }
            if (str3 != string.Empty)
            {
                olll.ReadPriv = str3;
            }
            if (this.EditUserId.Value != string.Empty)
            {
                olll.EditUser = this.EditUserId.Value;
            }
            if (this.DownloadUserId.Value != string.Empty)
            {
                olll.DownloadUser = this.DownloadUserId.Value;
            }
            if (this.ManageUserId.Value != string.Empty)
            {
                olll.ManageUser = this.ManageUserId.Value;
            }
            try
            {
                int num = dao.Insert(base.Conn, olll);
                if ((num != -1) && (path == string.Empty))
                {
                    path = Server.MapPath("/attachment/netdisk/diskpath" + num.ToString());
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    string commandText = "UPDATE [dbo].[Netdisk] SET [DiskPath] = @DiskPath WHERE [DiskId] = @DiskId ";
                    SqlParameter[] commandParameters = new SqlParameter[2];
                    commandParameters[0] = new SqlParameter("@DiskPath", SqlDbType.Text);
                    commandParameters[0].Value = path;
                    commandParameters[1] = new SqlParameter("@DiskId", SqlDbType.Int);
                    commandParameters[1].Value = num;
                    SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText, commandParameters);
                }
            }
            catch (Exception exception2)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception2);
                }
                this.litMessage.Text = base.ShowError("共享目录添加失败！", "new.aspx");
                return;
            }
            base.CloseConn();
            Response.Redirect("index.aspx");
           
        } 

    }
}
